/**
 * Created by hcadmin on 16/4/18.
 */
define([
    "jquery",
    "IO",
    "UPLOADUI",
    "UPLOAD",
    "jqueryUI",
    "bootbox"
], function ($, IO, upui, up, jui, bootbox, definebt) {
    $(function () {

        gettable(1);

        $("#adminadd").click(function(){
            var admin_name=$(".admin_name").val();
            var admin_phone=$(".admin_phone").val();
            var admin_password=$(".admin_password").val();
            var admin_password_confirm=$(".admin_password_confirm").val();
            var data=eval("(" + localStorage.getItem('data') + ")");
            //console.log();
            var admin_id=data.data.admin_id;
            var roleids='';
            $(".userRole:checked").attr("role_id")==undefined?'':roleids+=$(".userRole:checked").attr("role_id")+',';
            $(".doctorRole:checked").attr("role_id")==undefined?'':roleids+=$(".doctorRole:checked").attr("role_id")+',';
            $(".hospitalRole:checked").attr("role_id")==undefined?'':roleids+=$(".hospitalRole:checked").attr("role_id")+',';
            $(".consultRole:checked").attr("role_id")==undefined?'':roleids+=$(".consultRole:checked").attr("role_id")+',';
            $(".articleRole:checked").attr("role_id")==undefined?'':roleids+=$(".articleRole:checked").attr("role_id")+',';
            $(".adminRole:checked").attr("role_id")==undefined?'':roleids+=$(".adminRole:checked").attr("role_id")+',';
            $(".moneyRole:checked").attr("role_id")==undefined?'':roleids+=$(".moneyRole:checked").attr("role_id")+',';
            $(".directseedRole:checked").attr("role_id")==undefined?'':roleids+=$(".directseedRole:checked").attr("role_id")+',';
            $(".careRole:checked").attr("role_id")==undefined?'':roleids+=$(".careRole:checked").attr("role_id")+',';
            $(".homemakeRole:checked").attr("role_id")==undefined?'':roleids+=$(".homemakeRole:checked").attr("role_id")+',';
            roleids=roleids.substring(0,roleids.length-1);

            if(admin_name.length==0||admin_phone.length==0||admin_password.length==0){
                bootbox.alert("请输入完整信息");
                return;
            }
           if(admin_password!=admin_password_confirm){
               bootbox.alert("密码不一致,请重新输入");
               return;
           }

            var params = {
                admin_name:admin_name,
                admin_phone:admin_phone,
                admin_password:admin_password,
                admin_id:admin_id,
                roleids:roleids
            };

            IO.post("/api/admin/add",params,function(d){
                if(d.code=="0"){

                    $('#myModal').modal('hide');
                    var page=$(".page_control .li_num.selected").text();
                    gettable(page);
                }
                else {
                    $('#myModal').modal('hide');
                    alert(d.msg);
                }
            })

        });

        $("#adminedit").click(function(){
            var admin_name=$(".admin_name_edit").val();
            var admin_phone=$(".admin_phone_edit").val();
            //var admin_password=$(".admin_password_edit").val();
            //var admin_password_confirm=$(".admin_password_confirm_edit").val();
            var id=$("#hidden_admin_id").val();
            var data=eval("(" + localStorage.getItem('data') + ")");
            var admin_id=data.data.admin_id;

            var roleids='';
            $(".userRole_edit:checked").attr("role_id")==undefined?'':roleids+=$(".userRole_edit:checked").attr("role_id")+',';
            $(".doctorRole_edit:checked").attr("role_id")==undefined?'':roleids+=$(".doctorRole_edit:checked").attr("role_id")+',';
            $(".hospitalRole_edit:checked").attr("role_id")==undefined?'':roleids+=$(".hospitalRole_edit:checked").attr("role_id")+',';
            $(".consultRole_edit:checked").attr("role_id")==undefined?'':roleids+=$(".consultRole_edit:checked").attr("role_id")+',';
            $(".articleRole_edit:checked").attr("role_id")==undefined?'':roleids+=$(".articleRole_edit:checked").attr("role_id")+',';
            $(".adminRole_edit:checked").attr("role_id")==undefined?'':roleids+=$(".adminRole_edit:checked").attr("role_id")+',';
            $(".moneyRole_edit:checked").attr("role_id")==undefined?'':roleids+=$(".moneyRole_edit:checked").attr("role_id")+',';
            $(".directseedRole_edit:checked").attr("role_id")==undefined?'':roleids+=$(".directseedRole_edit:checked").attr("role_id")+',';
            $(".careRole_edit:checked").attr("role_id")==undefined?'':roleids+=$(".careRole_edit:checked").attr("role_id")+',';
            $(".homemakeRole_edit:checked").attr("role_id")==undefined?'':roleids+=$(".homemakeRole_edit:checked").attr("role_id")+',';
            roleids=roleids.substring(0,roleids.length-1);

            if(admin_id.length<=0){
                bootbox.alert("请重新点击编辑");
                return;
            }

            if(admin_name.length==0||admin_phone.length==0){
                bootbox.alert("请输入完整信息");
                return;
            }

            var params = {
                admin_name:admin_name,
                admin_phone:admin_phone,
                roleids:roleids,
                id:id,           //要修改的管理员的id
                admin_id:admin_id//做该操作的管理员id,当前登入的管理员id
            };

            IO.post("/api/admin/edit",params,function(d){
                if(d.code=="0"){
                    $('#myModaledit').modal('hide');
                    var page=$(".page_control .li_num.selected").text();
                    gettable(page);
                }
                else {
                    $('#myModaledit').modal('hide');
                    alert(d.msg);
                }
            })

        });

        $("#myModaledit").on("hidden.bs.modal", function() {
            $(this).removeData("bs.modal");
        })

    });

    $("#myModaledit").on("hidden.bs.modal", function() {
        $(this).removeData("bs.modal");
    });
    $("#myModaledit").on("hide", function() {
        $(this).removeData("modal");
    });

    function gettable(page){

        var grid;
        //每列数据列性
        var columns = [
            {
                title: '编号',
                dataIndex: 'admin_id',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>" + data[datarow].admin_id + "</span>";
                    //return "<span><img src='" + data[datarow].user_name + "' width='80px;' height='80px;'>&nbsp;&nbsp;</span>" + data[datarow].title + "</span>";
                }
            },
            {
                title: '姓名',
                dataIndex: 'admin_name',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>" + data[datarow].admin_name + "</span>";
                }
            },
            {
                title: '用户管理',
                dataIndex: 'userRole',
                dataSource: function (data, datarow, gridobj, current_column) {
                    var array=data[datarow].roleSet;
                    var ishave=false;
                    for(var i=0;i<array.length;i++){
                        if(array[i].name=='userRole'){
                            ishave=true;
                            continue;
                        }
                    }
                    if(ishave){
                        return "<span>" + '<img src="/images/96053/YES.png">' + "</span>";
                    }
                    else {
                        return "<span>" + '<img src="/images/96053/NO.png">'  + "</span>";
                    }
                }
            },
            {
                title: '医生管理',
                dataIndex: 'doctorRole',
                dataSource: function (data, datarow, gridobj, current_column) {
                    var array=data[datarow].roleSet;
                    var ishave=false;
                    for(var i=0;i<array.length;i++){
                        if(array[i].name=='doctorRole'){
                            ishave=true;
                            continue;
                        }
                    }
                    if(ishave){
                        return "<span>" + '<img src="/images/96053/YES.png">' + "</span>";
                    }
                    else {
                        return "<span>" + '<img src="/images/96053/NO.png">'  + "</span>";
                    }
                }
            },
            {
                title: '医院管理',
                dataIndex: 'hospitalRole',
                dataSource: function (data, datarow, gridobj, current_column) {
                    var array=data[datarow].roleSet;
                    var ishave=false;
                    for(var i=0;i<array.length;i++){
                        if(array[i].name=='hospitalRole'){
                            ishave=true;
                            continue;
                        }
                    }
                    if(ishave){
                        return "<span>" + '<img src="/images/96053/YES.png">' + "</span>";
                    }
                    else {
                        return "<span>" + '<img src="/images/96053/NO.png">'  + "</span>";
                    }
                }
            },
            {
                title: '直播管理',
                dataIndex: 'directseedRole',
                dataSource: function (data, datarow, gridobj, current_column) {
                    var array=data[datarow].roleSet;
                    var ishave=false;
                    for(var i=0;i<array.length;i++){
                        if(array[i].name=='directseedRole'){
                            ishave=true;
                            continue;
                        }
                    }
                    if(ishave){
                        return "<span>" + '<img src="/images/96053/YES.png">' + "</span>";
                    }
                    else {
                        return "<span>" + '<img src="/images/96053/NO.png">'  + "</span>";
                    }
                }
            },
            {
                title: '图文管理',
                dataIndex: 'articleRole',
                dataSource: function (data, datarow, gridobj, current_column) {
                    var array=data[datarow].roleSet;
                    var ishave=false;
                    for(var i=0;i<array.length;i++){
                        if(array[i].name=='articleRole'){
                            ishave=true;
                            continue;
                        }
                    }
                    if(ishave){
                        return "<span>" + '<img src="/images/96053/YES.png">' + "</span>";
                    }
                    else {
                        return "<span>" + '<img src="/images/96053/NO.png">'  + "</span>";
                    }
                }
            },
            {
                title: '家政管理',
                dataIndex: 'homemakeRole',
                dataSource: function (data, datarow, gridobj, current_column) {
                    var array=data[datarow].roleSet;
                    var ishave=false;
                    for(var i=0;i<array.length;i++){
                        if(array[i].name=='homemakeRole'){
                            ishave=true;
                            continue;
                        }
                    }
                    if(ishave){
                        return "<span>" + '<img src="/images/96053/YES.png">' + "</span>";
                    }
                    else {
                        return "<span>" + '<img src="/images/96053/NO.png">'  + "</span>";
                    }
                }
            },
            {
                title: '卡券管理',
                dataIndex: 'careRole',
                dataSource: function (data, datarow, gridobj, current_column) {
                    var array=data[datarow].roleSet;
                    var ishave=false;
                    for(var i=0;i<array.length;i++){
                        if(array[i].name=='careRole'){
                            ishave=true;
                            continue;
                        }
                    }
                    if(ishave){
                        return "<span>" + '<img src="/images/96053/YES.png">' + "</span>";
                    }
                    else {
                        return "<span>" + '<img src="/images/96053/NO.png">'  + "</span>";
                    }
                }
            },
            {
                title: '订单管理',
                dataIndex: 'consultRole',
                dataSource: function (data, datarow, gridobj, current_column) {
                    var array=data[datarow].roleSet;
                    var ishave=false;
                    for(var i=0;i<array.length;i++){
                        if(array[i].name=='consultRole'){
                            ishave=true;
                            continue;
                        }
                    }
                    if(ishave){
                        return "<span>" + '<img src="/images/96053/YES.png">' + "</span>";
                    }
                    else {
                        return "<span>" + '<img src="/images/96053/NO.png">'  + "</span>";
                    }
                }
            },
            {
                title: '管理员管理',
                dataIndex: 'adminRole',
                dataSource: function (data, datarow, gridobj, current_column) {
                    var array=data[datarow].roleSet;
                    var ishave=false;
                    for(var i=0;i<array.length;i++){
                        if(array[i].name=='adminRole'){
                            ishave=true;
                            continue;
                        }
                    }
                    if(ishave){
                        return "<span>" + '<img src="/images/96053/YES.png">' + "</span>";
                    }
                    else {
                        return "<span>" + '<img src="/images/96053/NO.png">'  + "</span>";
                    }
                }
            },
            {
                title: '资金管理',
                dataIndex: 'moneyRole',
                dataSource: function (data, datarow, gridobj, current_column) {
                    var array=data[datarow].roleSet;
                    var ishave=false;
                    for(var i=0;i<array.length;i++){
                        if(array[i].name=='moneyRole'){
                            ishave=true;
                            continue;
                        }
                    }
                    if(ishave){
                        return "<span>" + '<img src="/images/96053/YES.png">' + "</span>";
                    }
                    else {
                        return "<span>" + '<img src="/images/96053/NO.png">'  + "</span>";
                    }
                }
            },
            {
                title: '操作',
                dataIndex: 'action',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<a href='#' act_id="+data[datarow].admin_id+" class='editmodal' ><span style='color: #d47c7b;'>" + "编辑" + "</span></a>&nbsp;&nbsp;"+
                        "<a href='#' act_id="+data[datarow].admin_id+" class='delete'><span style='color: #6ab7ad;'>" + "删除" + "</span></a>";
                }
            }
        ];
        //数组初始化

        function dataInit() {

            grid = new Grid("/api/admin/list", $("#table_my_toast"), $("#paging_my_toast"), page, 10, columns, get_input_param(), 10);

            //初始化
            grid.Init();
        }

        //获取输入框内容
        function get_input_param() {

            var param="";
            return param;
        }
        dataInit();
    }

    $(document).on("click",".delete",function(){
        var act_id=$(this).attr("act_id");
        var data= eval("(" + localStorage.getItem('data') + ")");
        var admin_id=data.data.admin_id;
        if(act_id){
            bootbox.confirm("确定要删除吗？",function(result){
                if(result) {
                    IO.get("/api/admin/delete",{id:act_id,admin_id:admin_id},function(d){
                        if(d.code==0){
                            var page=$(".page_control .li_num.selected").text();
                            gettable(page);
                        }
                        else{
                            bootbox.alert('删除失败')
                        }
                    });
                }
            });
        }
    });

    //详情展示
    $(document).on("click",".editmodal",function(){
        var act_id=$(this).attr("act_id");
        //$("#adminadd").html('确认修改');
        //$("#textaddmanager").html('修改管理员');
        //console.log('要修改的',act_id);
        if(act_id){
            IO.get("/api/admin/detail",{id:act_id},function(d){
                if(d.code==0){

                    //$("#rows .row input[type=radio]:checked").each(function(){
                    //    $(this).removeAttr("checked");
                    //});
                    $(".admin_name_edit").val(d.data.admin_name);
                    $(".admin_phone_edit").val(d.data.admin_phone);
                    $("#hidden_admin_id").val(d.data.admin_id);

                    var roleset= d.data.roleSet;
                    var length=roleset.length;
                    var roleIDs=[];
                    for(var i=0;i<length;i++){
                        roleIDs[i]=roleset[i].role_id;
                    }
                    //console.log(roleIDs)
                    var allInput=[];

                    $("#rows .row input[type=radio]").each(function(){

                        if($(this).attr("class")){
                            //$(this).attr("checked","checked");
                            var roleid='';

                            if(!isNaN($(this).attr("role_id"))){

                                roleid=parseInt($(this).attr("role_id"));
                            }else {
                                roleid=$(this).attr("role_id");
                            }

                            var tag=roleIDs.indexOf(roleid);
                            if(tag!=-1){
                                //console.log(roleid);
                                //$(this).parent().next().next().children().removeAttr("checked");
                                $(this).prop("checked","checked");

                            }else{

                                $(this).parent().next().next().children().prop("checked","checked");
                            }
                        }
                    });

                    $('#myModaledit').modal();
                }
            });
        }

    });

});

